﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Linq;
using System.Json;
using System.Diagnostics;

namespace Aspire
{
    /// <summary>
    /// 框架配置类
    /// </summary>
    public class AspireConfig
    {
        /// <summary>
        /// 插件包集合
        /// </summary>
        public List<PluginPackage> Packages
        {
            get { return _packages; }
            set { _packages = value; }
        }
        private List<PluginPackage> _packages;

        /// <summary>
        /// 对应的系统配置信息
        /// </summary>
        public BusinessSystem System
        {
            get { return _system; }
            set { _system = value; }
        }
        private BusinessSystem _system;

        /// <summary>
        /// 解析配置文本串，生成配置对象
        /// </summary>
        /// <param name="strJson">配置文本串</param>
        public void Load(string strJson)
        {
            try
            {
                JsonObject jsonObject = JsonObject.Parse(strJson) as JsonObject;

                //生成插件包列表
                List<PluginPackage> packages = new List<PluginPackage>();
                Dictionary<long, PluginPage> pages = new Dictionary<long, PluginPage>();

                JsonArray pluginsArray = jsonObject["Plugins"] as JsonArray;

                foreach (var pluginObj in pluginsArray)
                {
                    PluginPackage package = new PluginPackage();
                    package.ID = pluginObj["ID"];
                    package.Name = pluginObj["Name"];
                    package.Url = pluginObj["URL"];

                    JsonArray pagesArray = pluginObj["PluginPages"] as JsonArray;
                    foreach (var pageObj in pagesArray)
                    {
                        PluginPage page = new PluginPage();
                        page.ID = pageObj["ID"];
                        page.ClassName = pageObj["ClassName"];

                        page.Package = package;
                        package.Pages.Add(page);

                        Debug.Assert(!pages.ContainsKey(page.ID));
                        pages[page.ID] = page;
                    }
                }

                this.Packages = packages;

                // 生成系统设置
                JsonObject systemObj = jsonObject["System"] as JsonObject;

                BusinessSystem system = new BusinessSystem();
                system.ID = systemObj["ID"];
                system.InternalName = systemObj["InternalName"];
                system.DisplayName = systemObj["DisplayName"];

                system.Functions = new List<Function>();
                JsonArray functionsArray = systemObj["Functions"] as JsonArray;

                List<Function> funcList = new List<Function>();

                foreach (var funcObj in functionsArray)
                {
                    Function func = new Function();

                    func.ID = funcObj["ID"];
                    func.Name = funcObj["Name"];
                    func.DisplayName = funcObj["DisplayName"];
                    func.Arguments = funcObj["Argument"];
                    func.ParentFunctionID = funcObj["ParentFunctionID"];

                    long pageID = funcObj["PluginPageID"];
                    Debug.Assert(pages.ContainsKey(pageID));

                    func.Page = pages[pageID];

                    if (func.ParentFunctionID == -1)
                    {
                        system.Functions.Add(func);
                    }
                    else
                    {
                        funcList.Add(func);
                    }

                    foreach (var item in system.Functions)
                    {
                        FillChildFunction(item, funcList);
                    }
                }

                this.System = system;
                this.Packages = packages;
            }
            catch (Exception ex)
            {
                throw new AspireException(ExceptionType.CONFIG_FILE_ERROR, "解析系统配置文件失败，请检查系统配置文件格式", ex);
            }
        }

        /// <summary>
        /// 递归填充子功能点
        /// </summary>
        /// <param name="parentFunc">父功能点</param>
        /// <param name="functions">子功能点</param>
        private void FillChildFunction(Function parentFunc, List<Function> functions)
        {
            var query = functions.Where(p => p.ParentFunctionID == parentFunc.ID).ToList();

            foreach (var item in query)
            {
                functions.Remove(item);
                parentFunc.Functions.Add(item);
                item.ParentFunction = parentFunc;

                FillChildFunction(item, functions);
            }

        }
    }
}
